﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

using _XNA__RunningDeath.DTO;
using _XNA__RunningDeath.GLO;
using _XNA__RunningDeath.DTO.VatThe;
using _XNA__RunningDeath.BUS.VatThe;

namespace _XNA__RunningDeath.BUS
{
    class CMap_BUS
    {
        CMap_DTO _map;
        
        ContentManager _Content;
        SpriteBatch _SpriteBatch;
        GameTime _GameTime;
        SoundEffect _sound;
       

        List<CChuoi_BUS> mutiChuoi = new List<CChuoi_BUS>();


        /// <summary>
        /// Tạo ra các trái chuối
        /// </summary>
        /// <param name="gameTime"></param>
        public void RandDChuoi(GameTime gameTime)
        {
            CChuoi_BUS mchuoi = new CChuoi_BUS(_Content,gameTime,_SpriteBatch);
            mutiChuoi.Add(mchuoi);
        }

        public CMap_BUS(ContentManager content, GameTime gameTime, SpriteBatch spriteBatch)
        {
            this._map = new _XNA__RunningDeath.DTO.CMap_DTO(content, gameTime, spriteBatch);
            
            _Content = content; _SpriteBatch = spriteBatch; _GameTime = gameTime;

            _sound = content.Load<SoundEffect>(@"AmThanh/correct");
            LoadBG();

            //if(CGLOBAL.NewGame == enumGame.mLoad)
            //{
            //    mutiChuoi = CThamSo.data.dsChuoi;
            //}
        }

        int demthoigian = 0;
        
        public void Update(GameTime gameTime)
        {
            RightToLeft(CThamSo.speedForm);

           demthoigian += gameTime.ElapsedGameTime.Milliseconds;
           if (demthoigian >= 500)
           {
               RandDChuoi(gameTime);
               demthoigian = 0;
           }

           for (int i = 0; i < mutiChuoi.Count; i++)
           {
                if(mutiChuoi[i].IsLife())
                {
                    if (CGLOBAL.NhanVatBus.recNhanVat.Intersects(mutiChuoi[i]._rechuoi))
                    {
                        _sound.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                        mutiChuoi[i].setDie();
                        CGLOBAL.Score += 1;
                    }
                }
           }


           foreach (CChuoi_BUS itemChuoi in mutiChuoi)
           {
               if(itemChuoi.IsLife())
                    itemChuoi.Update(gameTime);
           }

           CGLOBAL.dsChuoi = mutiChuoi;

        }

        public void Draw(GameTime gameTime)
        {
            this._map.Spritebath.Draw(this._map.Background1, this._map.Toado1, Color.White);
            this._map.Spritebath.Draw(this._map.Background2, this._map.Toado2, Color.White);
            
            foreach (CChuoi_BUS itemchuoi in mutiChuoi)
            {
                if( itemchuoi.IsLife() )
                    itemchuoi.Draw(gameTime);
            }
        }

        public void LoadBG()
        {
            this._map.Background1 = this._map.Content.Load<Texture2D>(@"GUI/bgGame");
            this._map.Background2 = this._map.Content.Load<Texture2D>(@"GUI/bgGame");
            this._map.Dstoado = this._map.Content.Load<Dictionary<string, Rectangle>>(@"GUI/ztoadoXMLgui");
            this._map.Toado1 = new Vector2(this._map.Dstoado["bgGame1"].X, this._map.Dstoado["bgGame1"].Y);
            this._map.Toado2 = new Vector2(this._map.Dstoado["bgGame2"].X, this._map.Dstoado["bgGame2"].Y);

        }

        /// <summary>
        /// Dịch chuyển Bg màn hình từ phải sang trái
        /// </summary>
        /// <param name="speed">Tốc độ dịch chuyển</param>
        public void RightToLeft(int speed)
        {
            this._map.Toado1 = new Vector2(this._map.Toado1.X - speed, this._map.Toado1.Y);
            if (this._map.Toado1.X <= -CThamSo.widthForm)
                this._map.Toado1 = new Vector2(CThamSo.widthForm, this._map.Toado1.Y);

            this._map.Toado2 = new Vector2(this._map.Toado2.X - speed, this._map.Toado2.Y);
            if (this._map.Toado2.X <= -CThamSo.widthForm)
                this._map.Toado2 = new Vector2(CThamSo.widthForm, this._map.Toado2.Y);
        }
       

        
        public void Stop()
        {
            CThamSo.speedForm = 0;
        }
    }
}
